package communication;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class Client {
	public static void main(String[] args) {
		new Client();
	}

	public Client() {
		try {
			int port = 1337;
			String hostname = "localhost";
			SocketFactory socketFactory = SSLSocketFactory.getDefault();
			SSLSocket socket = (SSLSocket) socketFactory.createSocket(hostname,
					port);
			// Create streams to securely send and receive data to the server
			InputStream in = socket.getInputStream();
			OutputStream out = socket.getOutputStream();

			// Read from in and write to out...
			Scanner scan = new Scanner(System.in);
			
			System.out
					.println("Choose action: \n0: Get medical records.\n1: Write to existing medical record.\n2: Remove medical record.\n3: Create new medical record.");
			System.out.println("----------------------------------------");
			int action = scan.nextInt();

			if (action == 0) {
				System.out
						.println("Enter social security number for target patient:");
				String nbr = scan.next();
				String toSend = "" + action + nbr;
				out.write(toSend.getBytes());
			}
			if (action == 1) {
				System.out
						.println("Enter social security number for target patient:");
				String nbr = scan.next();
				System.out.println("Enter ID of record to write to:");
				String id = scan.next();
				System.out.println("Enter message to add to record:");
				String message = scan.next() + scan.nextLine();
				String toSend = "" + action + nbr + id + "===" + message;
				out.write(toSend.getBytes());
			}
			if (action == 2) {
				System.out
						.println("Enter social security number for target patient:");
				String nbr = scan.next();
				String toSend = "" + action + nbr;
				out.write(toSend.getBytes());
			}
			if (action == 3) {
				System.out
						.println("Enter social security number for target patient:");
				String nbr = scan.next();
				System.out.println("Enter nurse's security number:");
				String nurseNbr = scan.next();
				System.out
						.println("Enter message to add to new record (can be empty)");
				String message = scan.next() + scan.nextLine();
				String toSend = "" + action + nbr + nurseNbr + message;
				out.write(toSend.getBytes());
			}
			int k = 0;
			while ((k = in.read()) != -1) {
				System.out.print((char) k);
			}

			// Close the socket
			in.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
